﻿using System.Windows.Forms;
using DesignerHelper.Addons;
using SpreadsheetGear;

namespace WorkbookDesigner.Addons.WorkbookExplorer
{
    public partial class Sheet : UserControl, IMarine<IPageSetup>
    {
        private IPageSetup Ps { get; set; }

        #region 封装变量及相应处理方法

        private string PrintArea
        {
            get { return textBoxPrintArea.Text; }
            set { textBoxPrintArea.Text = value; }
        }

        private string PrintTitleRows
        {
            get { return textBoxTitleRows.Text; }
            set { textBoxTitleRows.Text = value; }
        }

        private string PrintTitleColumns
        {
            get { return textBoxTitleColumns.Text; }
            set { textBoxTitleColumns.Text = value; }
        }

        private bool PrintGridline
        {
            get { return checkBoxGridline.Checked; }
            set { checkBoxGridline.Checked = value; }
        }

        private bool PrintBlackAndWhite
        {
            get { return checkBoxBlackAndWhite.Checked; }
            set { checkBoxBlackAndWhite.Checked = value; }
        }

        private bool PrintHeadings
        {
            get { return checkBoxRowColmunHeader.Checked; }
            set { checkBoxRowColmunHeader.Checked = value; }
        }

        private PrintLocation PrintComments
        {
            get
            {
                switch (comboBoxComment.SelectedIndex)
                {
                    case 0:
                        return PrintLocation.NoComments;

                    case 1:
                        return PrintLocation.SheetEnd;

                    case 2:
                        return PrintLocation.InPlace;
                }
                return PrintLocation.NoComments;
            }
        }

        private void InitPrintComment(PrintLocation pl)
        {
            switch (pl)
            {
                case PrintLocation.NoComments:
                    comboBoxComment.SelectedIndex = 0;
                    break;

                case PrintLocation.SheetEnd:
                    comboBoxComment.SelectedIndex = 1;
                    break;

                case PrintLocation.InPlace:
                    comboBoxComment.SelectedIndex = 2;
                    break;
            }
        }

        private void InitPrintOrder(Order order)
        {
            switch (order)
            {
                case Order.DownThenOver:
                    radioButtonDownOver.Checked = true;
                    radioButtonOverDown.Checked = false;
                    break;

                case Order.OverThenDown:
                    radioButtonOverDown.Checked = true;
                    radioButtonDownOver.Checked = false;
                    break;
            }
        }

        private Order PrintOrder
        {
            get
            {
                if (radioButtonDownOver.Checked && !radioButtonOverDown.Checked)
                    return Order.DownThenOver;
                else if (radioButtonOverDown.Checked && !radioButtonDownOver.Checked)
                    return Order.OverThenDown;
                else
                    return Order.DownThenOver;
            }
        }

        #endregion 封装变量及相应处理方法

        public Sheet()
        {
            InitializeComponent();
        }

        public Sheet(IPageSetup pageSetup) : this()
        {
            TriggerInit(pageSetup);
        }

        public void TriggerInit(IPageSetup pagesetup)
        {
            Ps = pagesetup;
            Init();
        }

        public void TriggerReset()
        {
            Init();
        }

        public void TriggerSet()
        {
            Set();
        }

        private void Init()
        {
            PrintArea = Ps.PrintArea;
            PrintTitleRows = Ps.PrintTitleRows;
            PrintTitleColumns = Ps.PrintTitleColumns;
            PrintGridline = Ps.PrintGridlines;
            PrintBlackAndWhite = Ps.BlackAndWhite;
            PrintHeadings = Ps.PrintHeadings;
            InitPrintComment(Ps.PrintComments);
            InitPrintOrder(Ps.Order);
        }

        private void Set()
        {
            Ps.Order = PrintOrder;
            Ps.PrintComments = PrintComments;
            Ps.PrintArea = PrintArea;
            Ps.PrintTitleColumns = PrintTitleColumns;
            Ps.PrintTitleRows = PrintTitleRows;
            Ps.PrintGridlines = PrintGridline;
            Ps.PrintHeadings = PrintHeadings;
            Ps.BlackAndWhite = PrintBlackAndWhite;
        }

        #region interface

        void IScv<IPageSetup>.TriggerReset()
        {
            TriggerReset();
        }

        void IScv<IPageSetup>.TriggerSet()
        {
            TriggerSet();
        }

        Control IScv<IPageSetup>.GetControl()
        {
            return this;
        }

        void IMarine<IPageSetup>.TriggerInit(IPageSetup sendIn)
        {
            TriggerInit(sendIn);
        }

        #endregion
    }
}